package com.hotchpotch.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.hotchpotch.R;
import com.hotchpotch.adapter.KillServiceAdapter;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.bean.KillServiceLogBean;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.ModelAdapter;

import java.util.List;

import butterknife.BindView;

public class KillServiceAct extends BaseActivity {
    private static final String TAG = "KillServiceAct";
    @BindView(R.id.act_killservice_recycler)
    RecyclerView recyclerView;
    @BindView(R.id.act_killservice_btn1)
    Button btn1;
    @BindView(R.id.act_killservice_btn2)
    Button btn2;

    private KillServiceAdapter adapter;
    private List<KillServiceLogBean> list;
    private ModelAdapter<KillServiceLogBean> modelAdapter = FlowManager.getModelAdapter(KillServiceLogBean.class);

    @Override
    protected int getLayoutId() {
        return R.layout.act_killservice;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new KillServiceAdapter(this, R.layout.item_killservice);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvents() {
        btn1.setOnClickListener(clickListener);
        btn2.setOnClickListener(clickListener);
    }

    @Override
    protected void initData() {
        showData();
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.act_killservice_btn1:
                    showData();
                    break;
                case R.id.act_killservice_btn2:
                    if (list.size() > 0 && list != null) {
                        modelAdapter.deleteAll(list);
                        showData();
                    }
                    break;
            }
        }
    };

    private void showData() {
        if (list != null) {
            list.clear();
        }
        list = SQLite.select().from(KillServiceLogBean.class).queryList();
        adapter.setData(list);
    }
}
